package demo1;

import java.util.List;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //图片平滑器
    public int[][] imageSmoother(int[][] img) {
        int n = img.length;
        int m = img[0].length;
        int[][] tmp = new int[n+2][m+2];
        int[][] ret = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                //if(i==0 || j==0 || i==n+1 || j==n+1) tmp[i][j] = 0;
                // else tmp[i][j] = img[i-1][j-1];
                tmp[i+1][j+1] = img[i][j];
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                int count = 9;
                int val = 0;
                for (int k = i-1; k <=i+1 ; k++) {
                    for (int l = j-1; l <=j+1 ; l++) {
                        if(k==0||l==0||k==n+1||l==m+1) count--;
                        val += tmp[k][l];
                    }
                }
                ret[i-1][j-1] = val/count;
            }
        }
        return ret;
    }


    public static void main1(String[] args) {
        int[] arr = {7,2,4,3};
        int[] arr2 = {5,6,4};
        ListNode l1 = new ListNode(2);
        ListNode h1 = l1;h1.next = new ListNode(7,new ListNode(4,new ListNode(3)));
        ListNode l2 = new ListNode(5,new ListNode(6,new ListNode(4)));
        addTwoNumbers(l1,l2);
    }

    //两数相加 2
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode ret = null;
        // l1 = rollback(l1);
        // l2 = rollback(l2);
        ListNode h1 = rollback(l1);
        ListNode h2 = rollback(l2);
        ListNode pcur = ret;
        int key = 0;
        while (h1 != null || h2 != null) {
            if(h1 != null && h2 != null){
                int val = (h1.val+h2.val+key)%10;
                if(pcur==null) {
                    pcur = new ListNode(val);
                    ret = pcur;
                }
                else pcur.next = new ListNode(val);
                if(h1.val+h2.val+key>9) key =1;
                else key = 0;
                h1 = h1.next;
                h2 = h2.next;
                if(pcur.next!=null) pcur = pcur.next;
            }else if(h1!=null&& h2==null){
                int val = (h1.val+key)%10;
                pcur.next = new ListNode(val);
                if(h1.val+key>9) key =1;
                else key = 0;
                h1 = h1.next;
                if(pcur.next!=null) pcur = pcur.next;
            }else if(h2!=null && h1==null){
                int val = (h2.val+key)%10;
                pcur.next = new ListNode(val);
                if(h2.val+key>9) key =1;
                else key = 0;
                h2 = h2.next;
                if(pcur.next!=null) pcur = pcur.next;
            }
        }
        if(key==1) {
            pcur.next = new ListNode(1);
            pcur = pcur.next;
        }
        return rollback(ret);
    }

    private static ListNode rollback(ListNode node){
        ListNode pcur = node;
        ListNode pNcur = node;
        ListNode prev = null;
        while (pcur != null) {
            pcur = pcur.next;
            pNcur.next = prev;
            prev = pNcur;
            pNcur = pcur;
        }
        return prev;
    }

    public ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
        ListNode h1 = l1;
        ListNode h2 = l2;
        int size1 = 0;
        int size2 = 0;
        while (h1 != null ) {
            h1 = h1.next;
            size1++;
        }
        while (h2 != null) {
            h2 = h2.next;
            size2++;
        }
        if(size1>=size2) {
            h1 = l1;
            h2 = l2;
        }else {
            h1 = l2;
            h2 = l1;
        }
        ListNode pcur1 = h1;
        ListNode pNcur = null;
        ListNode pcur2 = h2;
        while (size1 - size2 > 0) {
            size2++;
            pNcur = pcur1;
            pcur1 = pcur1.next;
        }
        if (size1 == size2) {
            if((pcur1.val + pcur2.val) > 9){
                pNcur = new ListNode(1);
                pNcur.next = pcur1;
                pcur1.val = (pcur1.val + pcur2.val)%10;
                pcur2.val = 0;
            }
        }
        while (pcur1 != null) {
            if ((pcur1.val + pcur2.val) > 9) {
                pNcur.val++;
                pcur1.val = (pcur1.val + pcur2.val)%10;
            }else {
                pcur1.val = pcur1.val+pcur2.val;
            }
            pNcur = pcur1;
            pcur1 = pcur1.next;
            pcur2 = pcur2.next;
        }

        return h1;
    }
}
